<template>
	<view class="apps">
		<view class="flex">
			<image :src="info.image" class="goods_image" mode="widthFix"></image>
		</view>
		<view class="flex mtb10 mlr20 bgwhite plr20 ptb20">
			<view class="f1 flex plr20 aic jcsb">
				<view class="ptb20 f14 black">{{info.title}}</view>
				<view class="pb20 red f16 b">¥{{info.price}}</view>
			</view>
			
		</view>
		<view class="flex mtb10 mlr20 bgwhite plr20 ptb20">
			<uv-parse :content="info.content" :tagStyle="style"></uv-parse>
		</view>
		<view class="fixd" v-if="showGow == 1">
			<uv-button type="error" text="立即购买"  @click="buy"></uv-button>
			<uv-safe-bottom></uv-safe-bottom>
		</view>
	</view>
</template>
<script>
	import { util } from '/common/util.js';
	let app;
	export default {
	  data() {
		return {
		  id:0,
		  info:{},
		  style:{
		  	'p':"font-size:28rpx;line-height:38rpx;",
		  	'img':'max-width:100%'
		  },
		  showGow:0,
		}
	  },
	  onLoad(option) {
		app = this;
		this.getConfig();
		this.goods(option.id);
	  },
	  methods: {
		async getConfig(){
			const data = await util.request({
					url:'/subscribe/config',
			});
			if(data.code == '200'){
				this.showGow = data.data;
			}
		},
		async goods(id){
		 const data = await util.request({
				url:'/subscribe/goods',
				data:{id:id}
			});
			if(data.code == '200'){
				this.info = data.data;
			}
		},
		async getUserinfo(){
			const data =  await util.request({
					url:"/mpwechat/userinfo",
				});
			return data;
		},
		async buy(){
			const info = this.info;
			this.goods(this.info.id);
			if(info.price != this.info.price){
				util.alert('此商品重新定价');
				return false;
			}
			const data =  await this.getUserinfo();
			if(data.code != '200'){
				uni.navigateTo({
					url:"/pages/member/login"
				})
				return false;
			}
			const payconfig =  await util.request({url:"/subscribe/buy",data:this.info,load:1});
			if(payconfig.code == '200'){
				wx.requestPayment({
					timeStamp: payconfig.data.payment.timeStamp,
					nonceStr: payconfig.data.payment.nonceStr,
					package: payconfig.data.payment.package,
					signType: payconfig.data.payment.signType,
					paySign: payconfig.data.payment.paySign,
					success (res) {
						util.alert('支付成功');
						setTimeout(function(){
							uni.redirectTo({
								url:"/pages/order/order?id="+payconfig.data.order.id
							})
						},1500)
					},
					fail (err) {
						if(err.errMsg === 'requestPayment:fail cancel'){
							util.alert('您取消了支付');
						}else{
							util.alert('支付失败');
						}
					},
					comlete(res){
						console.log(res);
					}
				})
			}
		}
	  }
	}
</script>

<style scoped>
	.goods_image{width:100%;}
	.fixd{position: fixed; bottom:0;left:0;right:0;}
</style>